package icraf.carbon.gui;

import icraf.carbon.core.Project;
import icraf.carbon.core.Zone.DataType;
import icraf.carbon.gui.MenuFactory.MenuProperty;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.table.TableModel;

public class InputBelowgroundEmission extends LCMatrixTabPanel {
	public class Listener implements ActionListener, FocusListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(projectBG)) {
				updateBG();
			} else if (e.getSource().equals(factorUse)||e.getSource().equals(factorNot)) {
				updateEFactor();
			} else if (MenuProperty.LIST_IMPORT_CLIPBOARD.isAction(e)) {
				clipboardPanel.updateClipboardData();
				JDialog d = clipboardPanel.createDialog((Window)getSelectedPanel().getRootPane().getParent(), ModalityType.APPLICATION_MODAL);
				Point loc = getSelectedPanel().getRootPane().getLocationOnScreen();
				d.setLocation(loc.x, loc.y);
				d.setVisible(true);
				if (clipboardPanel.isImport()) {
					importFromClipboard();
				}
			}
		}

		@Override
		public void focusGained(FocusEvent e) {
		}

		@Override
		public void focusLost(FocusEvent e) {
			if (e.getSource().equals(projectBG)) {
				updateBG();
			} else if (e.getSource().equals(factorUse)||e.getSource().equals(factorNot)) {
				updateEFactor();
			}
		}
	}

	private static final long serialVersionUID = 4305939712235366956L;
	public static final Color COLOR_E_FACTOR = new Color(150, 240, 90);
	
	private Listener listener = new Listener();
	private JCheckBox projectBG;
	private JRadioButton factorUse, factorNot;
	private LCClipboardPanel clipboardPanel = new LCClipboardPanel();
	
	public InputBelowgroundEmission() {
		super();
		projectBG = new JCheckBox();
		projectBG.addActionListener(listener);
		projectBG.addFocusListener(listener);
		JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p.add(new JLabel(MenuProperty.INPUT_INCLUDE_BG.getLabel()+":"));
		p.add(projectBG);
		
		JPanel fPanel = new JPanel();
		factorUse = new JRadioButton(MenuProperty.INPUT_EMISSION_FACTOR.getLabel(), true);
		factorNot = new JRadioButton(MenuProperty.INPUT_ACTUAL_EMISSION.getLabel(), false);
		factorUse.addActionListener(listener);
		factorUse.addFocusListener(listener);
		factorNot.addActionListener(listener);
		factorNot.addFocusListener(listener);
		ButtonGroup bg = new ButtonGroup();
		bg.add(factorUse);
		bg.add(factorNot);
		fPanel.add(factorUse);
		fPanel.add(factorNot);
		factorUse.setBackground(COLOR_E_FACTOR);
		fPanel.setBackground(new Color(230, 217, 85));
		p.add(fPanel);
		p.add(MenuFactory.createButtonIcon(MenuProperty.LIST_IMPORT_CLIPBOARD, listener));
		setTopComponent(p);
		
		clipboardPanel.setPreferredSize(new Dimension(600, 400));
	}

	@Override
	public void setProject(Project project) {
		super.setProject(project);
		setNegativeAllowed(true);
		projectBG.setSelected(project.isIncludeBelowgroundEmission());
		factorUse.setSelected(project.isUsingBelowgroundFactor());
		factorNot.setSelected(!project.isUsingBelowgroundFactor());
		clipboardPanel.setProject(project);
	}

	private void updateBG() {
		getProject().setIncludeBelowgroundEmission(projectBG.isSelected());
	}
	
	private void updateEFactor() {
		getProject().setUsingBelowgroundFactor(factorUse.isSelected());
		if(factorUse.isSelected()) {
			setDataType(DataType.BG_EMISSION_FACTOR,	null);
		} else {
			setDataType(DataType.BELOWGROUND_EMISSION,	null);
		}
	}

	@Override
	public void setDataType(DataType t, Object attributes) {
		if(getProject().isUsingBelowgroundFactor()) {
			super.setDataType(DataType.BG_EMISSION_FACTOR,	null);
		} else {
			super.setDataType(DataType.BELOWGROUND_EMISSION,	null);
		}
	}
	
	private void importFromClipboard() {
		MatrixPanel p = getSelectedPanel();
		LCTableModel m = (LCTableModel)p.getMatrixTableModel();
		if(clipboardPanel.isLCClipboard()) {
			
			TableModel tm = clipboardPanel.getLcTableModel();
			for(int r = 0; r < tm.getRowCount(); r++) {
				for(int c = 0; c < tm.getColumnCount(); c++) {
					m.setValueAt(tm.getValueAt(r, c), r, c);
				}
			}

		} else {
			
			String[][] strData = clipboardPanel.getData();
			if(strData == null) return;
			for(int r = 0; r < strData.length; r++) {
				for(int c = 0; c < strData[r].length; c++) {
					if(r < m.getRowCount() && c < m.getColumnCount())
						m.setValueAt(strData[r][c], r, c);
				}
			}
		}
		
	}
	
}
